package com.kim.smartring.net.entity;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

import com.kim.smartring.db.model.HeartRateRecord;
import com.kim.smartring.db.model.SleepRecord;
import com.kim.smartring.db.model.StepRecord;
import com.kim.smartring.net.IReceiverListener;

public class SyncEntity extends BaseEntity {

	public enum SyncType {
		/**
		 * 记步数据同步
		 */
		STEP,
		/**
		 * 睡眠数据同步
		 */
		SLEEP,
		/**
		 * 心率数据同步
		 */
		HEARTRATE;
	}

	private static final String TAG = "SyncEntity";

	private String sendData;
	private SyncType syncType;

	public SyncEntity(Context context, IReceiverListener receiverListener) {
		super(context, receiverListener);
	}

	public SyncEntity(Context context, SyncType syncType, String sendData,
			IReceiverListener receiverListener) {
		super(context, receiverListener);
		this.syncType = syncType;
		this.sendData = sendData;
	}

	@Override
	protected void init() {
		this.url = URLConstant.URL_SYNC_DATA;
	}

	@Override
	public String getSendData() {
		Log.d(TAG, String.format("sync type:%s", syncType.name()));
		Log.d(TAG, String.format("sync send data:$s", sendData));
		return sendData;
	}

	public static String generateSendDataStep(String userId,
			List<StepRecord> stepRecords) {
		JSONObject jsonObject = new JSONObject();
		String sendData = null;
		try {
			jsonObject.put("type", "syncStep");
			jsonObject.put("userId", userId);
			if (stepRecords != null && stepRecords.size() > 0) {

				JSONArray array = new JSONArray();
				JSONObject item = null;
				for (StepRecord record : stepRecords) {
					item = new JSONObject();
					item.put("stepId", record.getSid());
					item.put("stepCount", record.getsCount());
					item.put("stepDistance", record.getsDistance());
					item.put("stepCalories", record.getsCalories());
					item.put("stepTStart", record.getsTimeStart());
					item.put("stepTEnd", record.getsTimeEnd());
					item.put("synchronousTime", record.getSynchronousTime());
					array.put(item);
				}
				jsonObject.put("data", array);
			}
			sendData = jsonObject.toString();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			jsonObject = null;
		}
		return sendData;
	}

	public static String generateSendDataSleep(String userId,
			List<SleepRecord> sleepRecords) {
		JSONObject jsonObject = new JSONObject();
		String sendData = null;
		try {
			jsonObject.put("type", "syncSleep");
			jsonObject.put("userId", userId);
			if (sleepRecords != null && sleepRecords.size() > 0) {

				JSONArray array = new JSONArray();
				JSONObject item = null;
				for (SleepRecord record : sleepRecords) {
					item = new JSONObject();
					item.put("sleepId", record.getSid());
					item.put("sleepType", record.getsType());
					item.put("sleepTime", record.getsTime());
					item.put("synchronousTime", record.getSynchronousTime());
					array.put(item);
				}
				jsonObject.put("data", array);
			}
			sendData = jsonObject.toString();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			jsonObject = null;
		}
		return sendData;
	}

	public static String generateSendDataHR(String userId,
			List<HeartRateRecord> heartRateRecords) {
		JSONObject jsonObject = new JSONObject();
		String sendData = null;
		try {
			jsonObject.put("type", "syncHR");
			jsonObject.put("userId", userId);
			if (heartRateRecords != null && heartRateRecords.size() > 0) {

				JSONArray array = new JSONArray();
				JSONObject item = null;
				for (HeartRateRecord record : heartRateRecords) {
					item = new JSONObject();
					item.put("hrId", record.getHid());
					item.put("hrValue", record.gethValue());
					item.put("hrTime", record.gethTime());
					item.put("synchronousTime", record.getSynchronousTime());
					array.put(item);
				}
				jsonObject.put("data", array);
			}
			sendData = jsonObject.toString();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			jsonObject = null;
		}
		return sendData;
	}

}
